< previous page page_193 next page >

Page 193
This function acts like a combination of the Mid statement and the Instr function. You simply pass a string, the characters you want to search for, and what you want them replaced with. Using the Replace function, you can rewrite the code in Listing 9.6 as follows:
'Replace Line feeds with spaces
sInput = Replace(sInput, vbCr, " ")
'Replace carriage returns with spaces
sInput = Replace(sInput, vbLf, " ")
One possible use of the Replace function would be to remove quotation marks from a text entry field before placing the field value into a database.
Working with Specific Characters
A string is made up of individual characters. Each of these characters has a numeric code, known as the ASCII code. You can use ASCII codes with numeric operations on characters, or you can use them to add untypeable characters to a string, as shown in Listing 9.6. You use the Chr function to return a character that corresponds to a specific ASCII code. For example, the following two statements both print HELLO:
Print "HELLO"

Print Chr$(65) & Chr$(69) & Chr$(76) & Chr$(76) & chr$(79)
note.gif As with other functions mentioned earlier in this chapter, Chr has two forms: Chr() returns a Variant; Chr$() returns a string. square.gif

In the preceding example, typing HELLO is a lot simpler. However, you can't type some characters, such as line feeds and carriage returns, so you have to use the Chr function, as follows:
Print "Line 1" & Chr$(13) & Chr$(10) & "Line 2"
The carriage return/line feed combination deserves special mention. Frequently, you may find yourself using this combination to force a new line in a string, text box, or message box. For this purpose, Visual Basic has included an intrinsic constant, vbCrLf, so that the preceding line of code can be rewritten as follows:
Print "Line 1" & vbCrLf & "Line 2"
You also can use the Chr function to include quotation marks:
Print Chr$(34) & "This will be printed with quotes" & Chr$(34)
Print "this will not have quotes"
You can also use the Chr function to return any letter or number character. Table 9.3 lists some commonly used ASCII character codes.

 
< previous page page_193 next page >